﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using TravelSite.DAL.Repositories;
using TravelSite.Entities;

namespace TravelSite.Code
{
    public class BasePage : Page
    {
        public string JsCodeToRunOnReady { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            JsCodeToRunOnReady = "";
        }

        public bool IsAuthentificated
        {
            get { return System.Web.HttpContext.Current.User.Identity.IsAuthenticated; }
        }

        public string BasePath
        {
            get { return ResolveClientUrl("~"); }
        }

        public bool IsAdmin
        {
            get
            {
                if (CurrentProviderUserId != null)
                    return Roles.IsUserInRole("admin");

                return false;
            }

        }

        public int? CurrentUserId
        {
            get
            {
                var user = Membership.GetUser();
                if (user == null) return null;

                using (var userRepo = RepositoryFactory.ResolveRepository<UserRepository>())
                {
                    var findUser = userRepo.Find((Guid)user.ProviderUserKey);

                    Context.Session["userId"] = findUser.Id;
                    CurrentUserName = findUser.UserName;
                }

                return (int?)Context.Session["userId"];
            }
            set { Context.Session.Add("userId", value); }
        }

        public string CurrentUserName
        {
            get { return Context.Session["userName"] == null ? "" : Context.Session["userName"].ToString(); }
            set { Context.Session.Add("userName", value); }
        }

        public Guid? CurrentProviderUserId
        {
            get
            {
                var user = Membership.GetUser(CurrentUserName);
                if (user != null)
                    return (Guid?)user.ProviderUserKey;
                return (Guid?)null;
            }
        }

        protected string GetIPAddress()
        {
            /*string ipAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

            if (!string.IsNullOrEmpty(ipAddress))
            {
                string[] addresses = ipAddress.Split(',');
                if (addresses.Length != 0)
                {
                    return addresses[0];
                }
            }

            return Request.ServerVariables["REMOTE_ADDR"];*/

            return Request.UserHostAddress;
        }

        protected void AlertAndClear(string message)
        {
            Response.Clear();
            Response.Write(string.Format("<script type=\"text/javascript\">alert('{0}');</script>", message));
            Response.End();
        }

        public bool IsIE()
        {
            //return !((Request.Browser.Browser.ToLower() == "chrome") || (Request.Browser.Browser.ToLower() == "firefox"));
            return ((Request.Browser.Browser.ToLower() == "ie") || (Request.Browser.Browser.ToLower() == "mozilla"));
        }
       
    }
}